package xyz.xuminghai.javafx_learn.scene.chart;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedList;
import java.util.List;

/**
 * 2023/5/29 15:46 星期一<br/>
 * 折线图绘制一条连接一系列数据点的线
 * 数据点本身可以选择性地用符号表示。折线图通常用于查看随时间或类别变化的数据趋势
 *
 * @author xuMingHai
 */
public class LineChartApp extends Application {

    private final CategoryAxis xAxis = new CategoryAxis(FXCollections.observableList(new LinkedList<>()));

    private final NumberAxis yAxis = new NumberAxis(0.0, 100.0, 10.0);

    private final LineChart<String, Number> lineChart = new LineChart<>(xAxis, yAxis, FXCollections.observableList(new LinkedList<>()));


    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void init() {

        lineChart.setLegendVisible(false);
        lineChart.setCreateSymbols(false);
        lineChart.setAnimated(false);
        lineChart.setAxisSortingPolicy(LineChart.SortingPolicy.NONE);

        XYChart.Series<String, Number> series = new XYChart.Series<>();
        lineChart.setData(FXCollections.observableArrayList(List.of(series)));


        xAxis.setGapStartAndEnd(false);
        xAxis.setStartMargin(0.0);
        xAxis.setEndMargin(0.0);
        xAxis.setAnimated(false);

        ScheduledService<String> scheduledService = new ScheduledService<>() {
            private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("mm:ss:SSS");

            @Override
            protected Task<String> createTask() {
                return new Task<>() {
                    @Override
                    protected String call() {
                        return LocalTime.now().format(dateTimeFormatter);
                    }
                };
            }

            @Override
            protected void succeeded() {
                super.succeeded();
                String lastValue = super.getLastValue();
                ObservableList<String> categories = xAxis.getCategories();
                categories.add(lastValue);
                if (categories.size() > 10) {
                    categories.remove(0);
                }
                int value = (int) (Math.random() * 101);
                ObservableList<XYChart.Data<String, Number>> data = series.getData();
                data.add(new XYChart.Data<>(lastValue, value));
                if (data.size() > 10) {
                    data.remove(0);
                }
            }
        };

        scheduledService.setPeriod(Duration.millis(500.0));

        lineChart.setOnMouseClicked(event -> {
            if (scheduledService.isRunning()) {
                scheduledService.cancel();
            } else {
                scheduledService.reset();
                scheduledService.start();
            }
        });
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle(getClass().getSimpleName());
        Scene scene = new Scene(lineChart, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }


}
